using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace BrainWare
{
    class NumberKey
    {
        public int number;
        public GamePadKey key;
        public bool missing;

        public NumberKey(int Number, GamePadKey Key, bool Missing)
        {
            number = Number;
            key = Key;
            missing = Missing;
        }

    }

    class MissingNumberGame
    {
        private List<int> numbersAvailable;
        private List<NumberKey> numberKeysUsed;
        private List<GamePadKey> gamePadKeysAvailable; 
        private const int TimeBetweenNumbers = 2;
        private TimeSpan timeSinceLastNumber = new TimeSpan(0);
        private int missingNumber;
        
        
        private Random rand = new Random(323);
        
        private int currentNumber = -1;

        public MissingNumberGame()
        {
            numbersAvailable = new List<int>();
            numberKeysUsed = new List<NumberKey>();
            gamePadKeysAvailable = new List<GamePadKey>();
            ResetNumbersandKeys();
        }
        
        void ResetNumbersandKeys()
        {
            numbersAvailable.Clear();
            numberKeysUsed.Clear();
            gamePadKeysAvailable.Clear();

            for (int i = 1; i <= 10; i++)
            {
                numbersAvailable.Add(i);
            }                      

            gamePadKeysAvailable.Add(GamePadKey.A);
            gamePadKeysAvailable.Add(GamePadKey.B);
            gamePadKeysAvailable.Add(GamePadKey.X);
            gamePadKeysAvailable.Add(GamePadKey.Y);
        }
 
        public void Draw(RelativeSpriteBatch spriteBatch)
        {
            // Missing number not set so must be showing the numbers to be remembered
            if (numberKeysUsed.Count< 4)
            {
                // Draw the first current numbers
                Font.Draw(spriteBatch, FontStyle.Big, 580, 410, currentNumber);
            }
            else
            {
                for (int i = 0; i < 4; i++)
                {
                    NumberKey numberKey = numberKeysUsed[i];

                    switch (numberKey.key)
                    {
                        case GamePadKey.A:
                            Font.Draw(spriteBatch, FontStyle.Big, 200, 100, numberKey.number);
                            Font.Draw(spriteBatch, FontStyle.Big, 200, 100, numberKey.number);
                            break;
                        case GamePadKey.B:
                            Font.Draw(spriteBatch, FontStyle.Big, 200, 300, numberKey.number);
                            break;
                        case GamePadKey.X:
                            Font.Draw(spriteBatch, FontStyle.Big, 100, 200, numberKey.number);
                            break;
                        case GamePadKey.Y:
                            Font.Draw(spriteBatch, FontStyle.Big, 300, 200, numberKey.number);
                            break;


                    }
                }

            }

        }

        public void Update(GameTime gametime)
        {
            timeSinceLastNumber = timeSinceLastNumber.Add(gametime.ElapsedGameTime);
            
            // Is it time to get the next number?
            if (timeSinceLastNumber.Seconds >= TimeBetweenNumbers)
            {
                timeSinceLastNumber = new TimeSpan(0);

                // If we have shown the user the first 3 random numbers we need to get the new missing number
                if (numberKeysUsed.Count >= 3)
                {
                    if (missingNumber == 0)
                    {
                        missingNumber = GetNextNumber();
                        numberKeysUsed.Add(new NumberKey(missingNumber, GetNextGamePadKey(), true));
                    }
                }
                else
                {
                    // Get the next number to read for the player to remember
                    currentNumber = GetNextNumber();

                    numberKeysUsed.Add(new NumberKey(currentNumber, GetNextGamePadKey(), false));
                    Console.WriteLine(currentNumber.ToString());
                    Sound.Play(currentNumber.ToString());
                }
            }
        }

        private int GetNextNumber()
        {            
            int number =0;
            if (numbersAvailable.Count > 0)
            {
                int numberPos = rand.Next(numbersAvailable.Count);
                number = numbersAvailable[numberPos];                
                numbersAvailable.Remove(number);
            }
            return number;
        }

        private GamePadKey GetNextGamePadKey()
        {            
            GamePadKey gameKey = GamePadKey.Down;
            if (numbersAvailable.Count > 0)
            {
                int numberPos = rand.Next(gamePadKeysAvailable.Count);
                gameKey = gamePadKeysAvailable[numberPos];
                gamePadKeysAvailable.Remove(gameKey);
            }
            return gameKey;
        }

        private void PlayNextNumber()
        {
            throw new Exception("The method or operation is not implemented.");
        }
    }
}
